package com.support.android.myapp3;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastQQStyle;
import com.noober.background.BackgroundLibrary;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.support.android.myapp3.fragment.MainFragment;
import com.support.android.myapp3.fragment.PriceCalcFragment;
import com.support.android.myapp3.fragment.Tab2Fragment;
import com.support.android.myapp3.fragment.TabFragment;
import com.support.android.myapp3.fragment.Tax2Fragment;
import com.support.android.myapp3.fragment.TaxFragment;
import com.support.android.myapp3.fragment.Test2Fragment;
import com.support.android.myapp3.fragment.TestFragment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    FragmentManager fm;
    String currentFragmentStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        BackgroundLibrary.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        fm = getSupportFragmentManager();

        if (savedInstanceState != null) {
            currentFragmentStr = (String)savedInstanceState.getCharSequence("keyindex");
            Fragment to = fm.findFragmentByTag(currentFragmentStr);
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.show(to).commit();
            return;
        }

        showFragment(FragmentEnum.MAIN);

    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putCharSequence("keyindex", currentFragmentStr);
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if (id == R.id.action_backup) {
//            Toast.makeText(this, "backup", Toast.LENGTH_SHORT).show();
            Snackbar.make(getWindow().getDecorView(), "我是普通的Snackbar", Snackbar.LENGTH_LONG).show();
            return true;
        }

        if (id == R.id.action_delete) {
            Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
            showFragment(FragmentEnum.MAIN);
        } else if (id == R.id.nav_gallery) {
            showFragment(FragmentEnum.PRICE_CALC);
        } else if (id == R.id.nav_slideshow) {
            showFragment(FragmentEnum.TAX);
        } else if (id == R.id.nav_slideshow2) {
            showFragment(FragmentEnum.TAX2);
        } else if (id == R.id.nav_test) {
            showFragment(FragmentEnum.TEST);
        }else if (id == R.id.nav_tab) {
            showFragment(FragmentEnum.TAB);
        } else if (id == R.id.nav_tab2) {
            showFragment(FragmentEnum.TAB2);
        }else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    public void showFragment(FragmentEnum tag) {
        Fragment to = fm.findFragmentByTag(tag.getName());
        Fragment nowFrame = fm.findFragmentByTag(currentFragmentStr);

        if(to == null){
            switch (tag){
                case MAIN:
                    to = MainFragment.getInstance();
                    break;
                case PRICE_CALC:
                    to = PriceCalcFragment.getInstance();
                    break;
                case TAX:
                    to = TaxFragment.getInstance();
                    break;
                case TAX2:
                    to = Tax2Fragment.getInstance();
                    break;
                case TEST:
                    to = TestFragment.getInstance();
                    break;
                case TAB:
                    to = TabFragment.getInstance();
                    break;
                case TAB2:
                    to = Tab2Fragment.getInstance();
                    break;
            }
        }

        if (nowFrame == to) {
            return ;
        }

        FragmentTransaction transaction = fm.beginTransaction();

        if(nowFrame == null){
            Fragment ff = fm.findFragmentByTag(tag.getName());
            if(ff != null){
                return;
            }
            transaction.add(R.id.fl_main, to,tag.getName()).commit();
            currentFragmentStr = tag.getName();
            return;
        }

        if (!to.isAdded()&& to !=null) {//判断是否被添加到了Activity里面去了
            transaction.hide(nowFrame).add(R.id.fl_main, to,tag.getName()).commit();
        } else {
            transaction.hide(nowFrame).show(to).commit();
        }

        currentFragmentStr = tag.getName();

    }


}
